﻿using UnityEngine;

public static partial class Extensions
{
    /// <summary>
    /// 获取组件，不存在时自动创建
    /// </summary>
    public static T CheckComponent<T>(this GameObject gameObject) where T : Component
    {
        var comp = gameObject.GetComponent<T>();
        if (comp == null)
            comp = gameObject.AddComponent<T>();
        return comp;
    }

    /// <summary>
    /// 获取组件，不存在时自动创建
    /// </summary>
    public static T CheckComponent<T>(this Component component) where T : Component
    {
        var comp = component.GetComponent<T>();
        if (comp == null)
            comp = component.gameObject.AddComponent<T>();
        return comp;
    }
}